package Socket02;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 聊天室客户端
 */
public class Client {
    private Socket socket;

    public Client() {
        try {
            System.out.println("客户端等待建立中....");
            socket = new Socket("localhost", 8801);
            System.out.println("客户端已建立");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try (PrintWriter pw = new PrintWriter(
                new BufferedWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream())), true);) {
            Scanner scan = new Scanner(System.in);
            String s;
            while (true) {
                System.out.println("请输入：");
                s = scan.next();
                if ("exit".equals(s)) {
                    break;
                } else {
                    pw.println(s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}

